package cn.pink.gamesrv;

import cn.pink.common.id.IdAllotPool;
import cn.pink.core.Port;
import cn.pink.core.Service;
import cn.pink.core.support.Log;
import cn.pink.core.support.SysException;

/**
 * @Author: pink
 * @Date: 2022/8/1 19:13
 */
public class GamePort extends Port {
    /** ID分配池 */
    protected IdAllotPool idPool = new IdAllotPool(this);

    public GamePort(String portId) {
        super(portId);
    }

    @Override
    public long applySeqId() {
        // 未初始化id池
        if (idPool == null) {
            Log.error.error("本Port未初始化ID池：port={}", this);
            throw new SysException("本Port未初始化ID池：port={}", this);
        }

        return idPool.applyId();
    }

    /**
     * 覆写父类函数
     * 增加防ID重复的判断机制
     */
    @Override
    public void addService(Service service) {
        //先检查一下此ID之前是否已经添加过 避免由于ID冲突造成隐藏的BUG
        Service serv = getService(service.getId());
        if (serv != null) {
            throw new SysException("Port添加下属服务时发送重复的号码：ID={}", service.getId());
        }

        super.addService(service);
    }
}
